package cn.tedu.micro.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.Supplier;

//@Component
public class My02Filter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("这是进入的第一个过滤器");
        //请求相关的api
        ServerHttpRequest request = exchange.getRequest();
        //获取uri
        URI uri = request.getURI();
        System.out.println("获取到请求的URI："+uri);
        String host = uri.getHost();
        System.out.println("获取到请求的host："+host);
        String query = uri.getQuery();
        System.out.println("获取到请求的query："+query);
        //获取queryParams
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        //判断queryParams不为空
        if(!queryParams.isEmpty()){
            if (queryParams.containsKey("name")){
                List<String> names = queryParams.get("name");
                if(!CollectionUtils.isEmpty(names)){
                    System.out.println("query包含name，并且name的value为:"+names);
                }
            }
        }
        //获取请求头
        HttpHeaders headers = request.getHeaders();
        List<String> authorization = headers.get("Authorization");
        if(!CollectionUtils.isEmpty(authorization)){
            System.out.println("请求头中包含Authorization,内容为:"+authorization);
        }
        //获取cookie
        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
        if(!cookies.isEmpty()){
            //测试有没有一个cookie名为token
            if(cookies.containsKey("token")){
                List<HttpCookie> token = cookies.get("token");
                if(!CollectionUtils.isEmpty(cookies)){
                    System.out.println("请求中包含token,内容为:"+token);
                }
            }
        }
     //   return chain.filter(exchange);
        //获取响应的对象
        ServerHttpResponse response = exchange.getResponse();
        //直接拦截，在这个过滤器中将响应返回,准备一个要返回的字符串
        //{"state":200,"message":"你被拦截了","data":null}
        String jsonData="{\"state\":200,\"message\":\"你被拦截了\",\"data\":null}";
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        byte[] bytes = jsonData.getBytes(StandardCharsets.UTF_8);
        DataBuffer dataBuffer = response.bufferFactory().wrap(bytes);
        Mono<DataBuffer> dataBufferMono = Mono.fromSupplier(new Supplier<DataBuffer>() {
            @Override
            public DataBuffer get() {
                //返回上面包装好的缓冲流
                return dataBuffer;
            }
        });
       return response.writeWith(dataBufferMono);
    }

    @Override
    public int getOrder() {
        return 10;
    }
}
